package com.liu.yupao;
import java.util.*;
import java.util.concurrent.*;

import cn.hutool.core.date.StopWatch;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.liu.yupao.controller.UserController;
import com.liu.yupao.model.entity.User;
import com.liu.yupao.model.entity.UserTeam;
import com.liu.yupao.service.UserService;
import com.liu.yupao.service.UserTeamService;
import org.apache.commons.compress.utils.TimeUtils;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

import javax.annotation.Resource;

/**
 * @author lqh
 * @since 2025-08-30 0:18
 */
@SpringBootTest
public class Demo1 {

    @Resource
    private UserService userService;

    @Resource
    private UserController userController;

    @Resource
    private UserTeamService userTeamService;

    //指定线程池
    private ExecutorService executorService=new ThreadPoolExecutor(10,
            20,1000, TimeUnit.SECONDS,new ArrayBlockingQueue<> (10000));

    @Test
    public void test1(){
        System.out.println ();
    }
    @Test
    public void test2(){
        QueryWrapper<UserTeam> userTeamQueryWrapper = new QueryWrapper<> ();
        userTeamQueryWrapper.orderByAsc ("joinTime");
        List<UserTeam> list = userTeamService.list (userTeamQueryWrapper);
        System.out.println (list);
    }
    @Test
    public void test3(){
        long INSERT_NUM=10000;
        StopWatch stopWatch = new StopWatch ();
        stopWatch.start ();
        LinkedList<User> users = new LinkedList<> ();
        //查询数据
        for (long l = 0; l < INSERT_NUM; l++) {
            User user = new User ();
            user.setUsername("");
            user.setTags("");
            user.setProfile("");
            user.setUserAccount("3"+l);
            user.setUserAvatar("");
            user.setGender(0);
            user.setUserPassword("");
            user.setPhone("");
            user.setEmail("");
            user.setUserRole("");
            userService.save (user);
        }
        //批量插入
        userService.saveBatch (users,1000);
        stopWatch.stop ();
        System.out.println (stopWatch.getTotalTimeMillis ());

    }

    /**
     * 并发插入数据
     */
    @Test
    public void test04(){
        StopWatch stopWatch = new StopWatch ();
        stopWatch.start ();
        int j=0;
        int saveBatch=2500;
        List<CompletableFuture<Void>> futureList = new ArrayList<> ();
        for (int i = 0; i < 40; i++) {
            List<User> userList = Collections.synchronizedList (new ArrayList<> ());
                //分成十组插入数据
            do {
                j++;
                User user = new User ();
                user.setUsername ("");
                user.setTags ("");
                user.setProfile ("");
                user.setUserAccount ("");
                user.setUserAvatar ("");
                user.setGender (0);
                user.setUserPassword ("");
                user.setPhone ("");
                user.setEmail ("");
                userList.add (user);
            } while (j % saveBatch != 0);
                    //异步执行
                    CompletableFuture<Void> future = CompletableFuture.runAsync (() -> {
                        System.out.println ("threadName=>"+Thread.currentThread ().getName ());
                        userService.saveBatch (userList, saveBatch);
                    },executorService);
                    futureList.add (future);
        }
        //等待所有线程执行完成之后，才算结束
        CompletableFuture.allOf (futureList.toArray (new CompletableFuture[]{})).join ();
        stopWatch.stop ();
        System.out.println ("耗时时间"+stopWatch.getLastTaskTimeMillis ());

    }
}
